﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThouShaltNotBrickLevelEditor.ViewModels;
using System.ComponentModel;
using GMUtilitiesDotNet.Services.ServiceInterfaces;
using GMUtilitiesDotNet.ViewModels;

namespace ThouShaltNotBrickLevelEditor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Constructors
        public MainWindow()
        {
            InitializeComponent();
        }
        #endregion

        #region Event Handlers
        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            // really ugly but what can you do?
            MainWindowViewModel mwvm = DataContext as MainWindowViewModel;
            if (mwvm != null)
            {
                mwvm.RequestCloseMainWindow += (send, args) => Close();
            }
        }

        private void WindowClosing(object sender, CancelEventArgs e)
        {
            // really ugly but what can you do?
            MainWindowViewModel mwvm = DataContext as MainWindowViewModel;
            if (mwvm != null)
            {
                CustomDialogResults messageBoxResult = CustomDialogResults.Yes;
                if (mwvm.IsPendingLevelChanges())
                {
                    messageBoxResult = ViewModelBase.ServiceProvider.Resolve<IMessageBoxService>().ShowYesNo(
                        "There are unsaved level changes that will be lost. Are you sure you wish to continue?",
                        CustomDialogIcons.None);
                }

                if (messageBoxResult != CustomDialogResults.Yes)
                {
                    e.Cancel = true;
                }
            }
        }
        #endregion
    }
}